#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.IO;
using System.Net;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Caching;
using octalforty.Componento.ComponentModel.Installation;
using octalforty.Componento.ComponentModel.Installation.Web;
using octalforty.Componento.ComponentModel.Tracing;
using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation
{
    [TestFixture()]
    public class HttpFileResourceProviderTestFixture
    {
        [Test()]
        public void GetResourcesWithContentDisposition()
        {
            var mockRepository = new MockRepository();

            var cacheManager = mockRepository.StrictMock<ICacheManagerService>();
            var httpRequestFactory = mockRepository.StrictMock<IHttpRequestFactoryService>();
            var httpRequest = mockRepository.StrictMock<IHttpRequest>();
            var httpResponse = mockRepository.StrictMock<IHttpResponse>();
            var headers = new WebHeaderCollection();
            headers["Content-Disposition"] = "attachment;filename=bar.zip";

            using(mockRepository.Record())
            {
                Expect.Call(cacheManager.GetCacheEntryInfo("http://foo.com/?id=foo")).
                    Return(null);

                Expect.Call(httpRequestFactory.CreateHttpRequest(HttpMethod.Get, new Uri("http://foo.com/?id=foo"))).
                    Return(httpRequest);

                Expect.Call(httpRequest.GetResponse()).
                    Return(httpResponse);

                Expect.Call(httpResponse.StatusCode).
                    Return(HttpStatusCode.OK).
                    Repeat.Any();

                Expect.Call(httpResponse.StatusDescription).
                    Return("OK");

                Expect.Call(httpResponse.ContentLength).
                    Return(0).
                    Repeat.Any();

                Expect.Call(httpResponse.GetResponseStream()).
                    Return(new MemoryStream(new byte[] { 1, 2 }));

                Expect.Call(delegate { cacheManager.Add("http://foo.com/bar.zip?id=foo", new byte[] { 1, 2 }); });

                Expect.Call(httpResponse.Headers).
                    Return(headers).
                    Repeat.Any();

                Expect.Call(delegate { ((IDisposable)httpResponse).Dispose(); });
            } // using

            HttpFileResourceProvider httpFileResourceProvider = new HttpFileResourceProvider(new TraceManagerService());
            httpFileResourceProvider.CacheManager = cacheManager;
            httpFileResourceProvider.HttpRequestFactory = httpRequestFactory;

            Uri[] resources = httpFileResourceProvider.GetResources(new Uri("http://foo.com/?id=foo"), "foo", null);

            Assert.AreEqual(1, resources.Length);
            Assert.AreEqual(new Uri("cache+http://foo.com/bar.zip?id=foo"), resources[0]);
        }
    }
}
